import type { User } from '@/types'
import { api } from './api'
import axios from 'axios'

interface LoginForm {
  username: string
  password: string
}

interface LoginResponse {
  user: User
  token: string
}

interface StatusResponse {
  needsSetup: boolean
}

interface SetupAdminRequest {
  username: string
  password: string
  nginxConfig?: {
    nginxExec?: string
    nginxCwd?: string
    nginxConfDir?: string
    isDockerized?: boolean
    dockerContainerName?: string
    dockerHostPath?: string
    dockerContainerPath?: string
    dockerHostLogPath?: string
    dockerContainerLogPath?: string
    dockerHostSslPath?: string
    dockerContainerSslPath?: string
  }
}

export const authService = {
  // 登录
  async login(data: LoginForm): Promise<LoginResponse> {
    const response = await api.post('/auth/login', data)
    return response.data
  },

  // 获取当前用户信息
  async getCurrentUser(): Promise<User> {
    const response = await api.get('/auth/me')
    return response.data
  },

  // 检查系统状态
  async checkStatus(): Promise<StatusResponse> {
    const response = await api.get('/auth/status')
    return response.data
  },

  // 初始化管理员账号
  async setupAdmin(data: SetupAdminRequest): Promise<LoginResponse> {
    const response = await api.post('/auth/setup', data)
    return response.data
  }
}

export const setupAdmin = async (data: SetupAdminRequest) => {
  const response = await axios.post('/api/auth/setup', data)
  return response.data
}

export const login = async (username: string, password: string) => {
  const response = await axios.post('/api/auth/login', { username, password })
  return response.data
}

export const checkAuth = async () => {
  try {
    const response = await axios.get('/api/auth/check')
    return response.data
  } catch (error) {
    return { authenticated: false }
  }
} 